package com.reviewit.mbean;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.bson.types.ObjectId;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

import com.reviewit.dao.AnexoDao;
import com.reviewit.model.Anexo;

@ManagedBean  
@SessionScoped  
public class AnexoBean implements Serializable {  
	private static final long serialVersionUID = 1L;
	
	private Anexo anexo;
	private AnexoDao anexoDAO;
		
	public AnexoBean() {
		super();
		this.anexoDAO = AnexoDao.getInstance();
		this.anexo = null;
	}
    
    public StreamedContent getAnexo(String id) throws FileNotFoundException
    {
   		anexo = anexoDAO.findOne("idDocument", new ObjectId(id));
    	return new DefaultStreamedContent(new ByteArrayInputStream(anexo.getFile()), "application/pdf", anexo.getName()); 
    }
   
}